#!/usr/bin/env python3
import pika
import time
import json
from subprocess import Popen, PIPE
import sqlite3
import sys
import socket


def sendFaceDbOperateMsg(udpmsg):
    # 建立连接
    credentials = pika.PlainCredentials('aiot', 'aiot')
    Parameter = pika.ConnectionParameters('127.0.0.1', 5672, '/', credentials)
    connection = pika.BlockingConnection(Parameter)
    channel = connection.channel()

    # 声明/指定消息队列
    channel.queue_declare(queue='PDMoutQueue')

    channel.basic_publish(exchange='',
                          routing_key='PDMoutQueue',
                          body=udpmsg)
    print("msg: ", udpmsg)
    print(" [x] Sent msg out!")


if __name__ == '__main__':
    '''
    发布img imgpath指令至rabbitMQ服务器
    '''
    # udp socket
    BUFSIZE = 1024
    server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    #ip_port = ('10.39.245.93', 9998)
    ip_port = ('127.0.0.1', 9998)
    server.bind(ip_port)

    # udp server
    while True:
        data, client_addr = server.recvfrom(BUFSIZE)
        print("from PassDB manager get: ", data)
    
        # 生成调用指令消息内容,发布消息
        sendFaceDbOperateMsg(data)

    server.close()
